/////////////////////////////// PROPERTY ///////////////////////////////
var properties = new Array();

var fillProperty = function(data){
	$("#propertyName").val(data == undefined ? "":data.name);
	if(data.type == "ENUMARATION"){
	    var combo = "<select class='system' name='value' size='1'>";	
		$.each(data.typeValues, function(index, entry) {
			combo += "<option value='" + entry + "'";
			if(data != undefined && data.value == entry)
				combo += " selected";
			combo += ">" + entry;
			combo += "</option>";	
		});
		combo += "</select>";
		$("#propertyValueColumn").html(combo);
	} else {
        var inputField = "<input id=\"propertyValue\" name=\"value\" type=\"text\" size=\"50\" maxlength=\"50\" value=\"";
        inputField += data == undefined ? "":data.value;
        inputField += "\"/>";
		$("#propertyValueColumn").html(inputField);
	}
	$('#propertySubmitter').attr("disabled", false);
	$('#modifyPropertyResult').html("");
};

function checkProperty(){
	var propName = $("#propertyName").val();
	var propValue = $("#propertyValue").val();
	var result = true;
	$.each(properties, function(index, entry) {
		if(entry.name == propName){
			var intReg = new RegExp("^[0-9]+$");
			var doubleReg = new RegExp("^[0-9]+[\.]?[0-9]+$");
			var argbReg = new RegExp("^[0-9ABCDEF]{8}$");
			if(entry.type == "INTEGER" && !intReg.test(propValue)){
				alert("Please enter a valid integer value for property '" + propName + "' !");
				result = false;
			} else if (entry.type == "DOUBLE" && !doubleReg.test(propValue)){
				alert("Please enter a valid double value for property '" + propName + "' !");
				result = false;
			} else if (entry.type == "ARGBCOLOR" && !argbReg.test(propValue)){
				alert("Please enter a valid ARGB value (8 HEX digits) for property '" + propName + "' !");
				result = false;
			} else {
				// It's a string or enumeration
			}
		}
	});
	return result;
}

var saveAvailableProperties = function(data){
	properties = new Array();
	  $.each(data, function(index, entry) {
		  properties.push(entry);
	  });
};

var refreshAvailablePropertiesHandler = function(_data){
  while ($('#AvailableProperties tbody').children().length > 1) {
    $('#AvailableProperties tr:nth-child(2)').remove();
  }

  saveAvailableProperties(_data);

  $.each(_data, function(index, entry) {
	    var background = '';
	    if(index % 2 == 0){
	        background = " style='background-color: #ececec;'";
	    }
	    var row = $('<tr' + background + '>').appendTo('#AvailableProperties');
    $('<td>')
      .html(entry.value)
      .prependTo(row);
    $('<td>')
    .html(entry.type)
    .prependTo(row);
    $('<td>')
      .html(entry.name)
      .prependTo(row);
    $("<td>")
      .addClass('action')
      .html('edit')
      .click(function() {
      	$('#propertyModifierForm').removeClass('hidden');  // Show main window
    	$('#propertyModifierFormTitle').html('Modify property entry: ' + entry.name);  // Show main window
    	$('#propertySubmitter').val('update');
        fillProperty(entry);
       })
      .appendTo(row);
  });
};

var refreshAvailableProperties = function(){
  $.ajax({
    dataType: 'json',
    url: '/ajax/getPropertyDefinitions',
    success: refreshAvailablePropertiesHandler,
    error: function(ev) {
    }
  });
};

$(document).ready(function() {
	$('#propertyModifier').ajaxForm({
		dataType: 'json',
		beforeSubmit: function() {
			var everythingIsOk = checkProperty();
			if(everythingIsOk){
				enableRefresh = false;
				$('#propertySubmitter').attr("disabled", true);
			}
			else
				return false;
		},
		success: function(data){
			enableRefresh = true;

			var $out = $('#modifyPropertyResult');
			$out.removeClass('errorMsg');
			$out.removeClass('warnMsg');
			if(data.status = 'WARN'){
				$out.addClass('warnMsg');
			} else if(data.status = 'ERROR'){
				$out.addClass('errorMsg');
			}

			$out.append(data.message);
			refreshAvailableProperties();
		}
	});
});
/////////////////////////////// PROPERTY END ///////////////////////////////
